package org.noranj.test;


import java.io.File;
import java.io.InputStream;
import java.net.URL;

import org.apache.cxf.helpers.IOUtils;
import org.apache.cxf.io.CachedOutputStream;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.FileEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.noranj.dao.ConversationDAO;
import org.noranj.dto.ConversationDTO;
import org.noranj.service.ConversationManager;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {

	public static void main44(String[] arg){
		ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");
		ConversationManager conversationManager = (ConversationManager) ctx.getBean("conversationManagerImpl");
//testing  Insert
		ConversationDTO conversation = new ConversationDTO();		 
		conversation.setUserId("2");
		conversation.setConversationText("Salam donya vasta man mikham piyadeh shi?");
		conversationManager.insertConversation(conversation);
		//TEsting getlist
		System.out.println(	conversationManager.getConversations().get(0).getConversationText());
	}
	
	 public static void getListConversation()  throws Exception {
	    System.out.println("Sent HTTP GET request to query user info");
	    URL url = new URL("http://localhost:9000/conversation/conversations");
	    InputStream in = url.openStream();
	    System.out.println(getStringFromInputStream(in));
	  } 
	 
	public static void main(String[] arg) throws Exception {
	  //getListConversation();
	  add();
	}
	
	private static void add() throws Exception {
    // Sent HTTP POST request to add user
    Main client = new Main();
    System.out.println("\n");
    System.out.println("Sent HTTP POST request to add user");
    String inputFile = client.getClass().getResource("add_user.xml").getFile();
    File input = new File(inputFile);
    HttpPost post = new HttpPost("http://localhost:9000/conversation/addrow");
    post.addHeader("Accept" , "text/xml");
    HttpEntity entity = new FileEntity(input, "text/xml; charset=ISO-8859-1");
    post.setEntity(entity);
    HttpClient httpclient = new DefaultHttpClient();

    try {
      HttpResponse result = httpclient.execute(post);
        System.out.println("Response status code: " + result.getStatusLine());
        System.out.println("Response body: ");
        System.out.println(getStringFromInputStream(result.getEntity().getContent()));
    } finally {
        // Release current connection to the connection pool once you are
        // done
    }

  }
	
	private static String getStringFromInputStream(InputStream in) throws Exception {
    CachedOutputStream bos = new CachedOutputStream();
    IOUtils.copy(in, bos);
    in.close();
    bos.close();
    return ">>>>\r\n"+bos.getOut().toString()+"\r\n-----------------------------------------------------------";
  }
	
}
